<?php

// amespace Library\ModuleClass;
class Module {

    static $DB;
    public $Db;
    protected $_table;

    public function __construct() {
        if (empty(self::$DB)) {
            self::$DB = new DataBase();
        }
        $this->Db = self::$DB;
    }

    /**
     * @desc 添加一条记录
     * 
     * @param string $table
     * @param array $addField
     * @return int
     *   */
    public function save($table, $addField) {
        $addField = $this->checkFields($addField, $this->getTableFields($table));
        return $this->Db->insert($table, $addField, true);
    }

    /**
     * @desc 更新一条记录
     * 
     * @param string $table
     * @param array $editField
     * @param array $where
     * @return int
     *   */
    public function update($table, $editField, $where) {
        $editField = $this->checkFields($editField, $this->getTableFields($table));
        return $this->Db->update($table, $editField, $where);
    }

    /**
     * @desc 删除一条记录
     * 
     * @param string $table
     * @param array $where 
     * @return int
     *  */
    public function delete($table, $where) {
        return $this->Db->delete($table, $where);
    }

    /*
     * 分页查询数据
     * @RETURN array 单条数据数组
     */

    public function getList($table, $fields = '*', $where = null, $limit = null, $group = null, $order = null, $output = false) {
        return $this->Db->FetchAll($table, $fields, $where, $limit, $group, $order, $output);
    }

    /*
     * 获取单条信息详情
     * @RETURN array 单条数据数组
     */

    public function getOneRow($table, $fields = "*", $where = null) {
        if (empty($where)) {
            return array();
        }
        return $this->Db->FetchOneRow($table, $where, $fields, $output = false);
    }
    
    public function getRow($table, $fields = "*", $where = null, $group = null, $order = null, $output = false){
        if (empty($where)) {
            return array();
        }
        return $this->Db->FetchRow($table, $fields, $where, $group, $order, $output);
    }

    /*
     * 根据条件获取数据条数
     * @return int 条数
     */

    public function getRowCount($table, $where = array(), $fields = 'COUNT(*)') {
        if (empty($where)) {
            return array();
        }
        return $this->Db->FetchOne($table, $where, $fields);
    }

    /**
     * 开始事务
     * @return type
     */
    public function beginTransaction() {
        return $this->Db->BeginTransaction();
    }

    /**
     * 提交事务
     * @return type
     */
    public function commit() {
        return $this->Db->Commit();
    }

    /**
     * 事务回滚
     * @return type
     */
    public function rollBack() {
        return $this->Db->RollBack();
    }
    
    /**
     * 
     * @param string $table
     * @return array
     */
    public function getTableFields($table) {
        Cache::Set('table', 'fields', 900, 0);
        if (!$fields_cache = Cache::Get('table_' . $table)) {
            $fields = $this->Db->ShowColumns($table);
            Cache::Save('table_' . $table, $fields);
        } else {
            $fields = $fields_cache;
        }
        return $fields;
    }
    
    /**
     * 数据重组
     * @param array $data
     * @param array $fields
     * @return array
     */
    public function checkFields($data = array(), $fields = array()) {
        foreach ($data as $k => $val) {
            if (!in_array($k, $fields)) {
                unset($data[$k]);
            }
        }
        return $data;
    }
}